package com.leecode.base;


import java.util.HashMap;
import java.util.TreeMap;

/**
 * @Author: yaoshubin
 * @Date: 2022-12-11 15:02
 * @description : 初始Hash 的 按值传递
 * <p>
 * Dubble ，Integer ，String 等 JAVA 原生的类型，都是值传递
 * <p>
 * 但是引用类型 就是按引用传递 ，除非你自己重写hashCode和equals  方法
 */
public class Index_06_Hash {

    public static void main(String[] args) {

        //基础类型
        HashMap<Object, Object> map1 = new HashMap<>();


        Integer t1 = 123445;
        Integer t2 = 123445;
        System.out.println(t1 == t2);
        map1.put(t1, "v1");
        System.out.println(map1.containsKey(t2));


        System.out.println("++++++++++++++++++");


        map1.put("k", "v1");
        map1.put("k", "v2");

        String str1 = "v1";
        String str2 = "v1";

        map1.put(str1, "v1");

        System.out.println(map1.containsKey(str1));
        System.out.println(map1.containsKey(str2));

        System.out.println("-------------------");


        //引用类型 String 中重写hashCode 和 equals
        String s1 = new String("111");
        String s2 = new String("111");
        map1.put(s1, "v1");
        System.out.println(map1.containsKey(s1));
        System.out.println(map1.containsKey(s2));  //true or false?

        System.out.println("===============");


        //其他引用类型
        User u1 = new User(30, "张飞");
        User u2 = new User(30, "张飞");

        map1.put(u1, "张飞");
        System.out.println(map1.containsKey(u1));
        System.out.println(map1.containsKey(u2)); //true or false?


        System.out.println("=======treeMap========");
        //TreeMap 有序表 ，JAVA钟是红黑树  ，时间复杂度 O(logN)
        TreeMap<Integer,String> treeMap = new TreeMap();
        treeMap.put(1,"11");
        treeMap.put(8,"88");
        treeMap.put(7,"77");
        treeMap.put(4,"44");
        treeMap.put(1,"66");
        treeMap.put(2,"22");

        System.out.println(treeMap.get(1));
        System.out.println(treeMap.firstKey());  //因为有序才支持，HashMap没有
        System.out.println(treeMap.lastKey());
        System.out.println(treeMap.ceilingKey(4)); // >=  4 最近的Key
        System.out.println(treeMap.ceilingKey(3)); // >=  3 最近的Key
        System.out.println(treeMap.floorKey(3));  // <= 3 最近的Key

        TreeMap<User,String> userTreeMap = new TreeMap();
        User user1 = new User();
        User user2 = new User();
        userTreeMap.put(user1,"user1");
        userTreeMap.put(user2,"user2");

        //base.User cannot be cast to java.lang.Comparable  ,因为不知道如何比较
        System.out.println(userTreeMap.get(user1));

    }
}
